//
//  ZJPictureBrowser.swift
//  PictureBrowser
//
//  Created by zhengju on 2017/7/27.
//  Copyright © 2017年 zhengju. All rights reserved.
//

import UIKit

 class ZJPictureBrowser: UIViewController {

    public  var pictureNames = [String]()
    
    
    public var pictureUrls = [String]()
    
    var currentCell: ZJPictureBrowserCell?
    
    
    /// 本VC的presentingViewController
 
    
    public  var presentingVC: UIViewController? = nil
    
    ///
    
    var flowLayout: ZJPictureBrowserLayout?
    
    /// 容器
    
    var collectionView: UICollectionView?

    
    var downloadBtn: UIButton!
    
   
    override  func viewDidLoad() {
        super.viewDidLoad()

        
        flowLayout = ZJPictureBrowserLayout()
       
        self.collectionView = UICollectionView(frame: CGRect.zero, collectionViewLayout: flowLayout!)

        collectionView?.frame = view.bounds
        collectionView?.delegate = self
        collectionView?.dataSource = self
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.isPagingEnabled = true
        collectionView?.register(ZJPictureBrowserCell.self, forCellWithReuseIdentifier: NSStringFromClass(ZJPictureBrowserCell.self))
        view.addSubview(collectionView!)
        
        downloadBtn = UIButton()
        downloadBtn.frame = CGRect.init(x: view.bounds.size.width - 40, y: view.bounds.size.height - 40, width: 35, height: 35)
        downloadBtn.setImage(UIImage.init(named: "下载"), for: .normal)
        downloadBtn.addTarget(self, action: #selector(downloadClick(button:)), for: .touchUpInside)
        view.addSubview(downloadBtn)
    }
    
    //MARK:下载图片
    func downloadClick(button:UIButton) {
        
        //找出当前显示的cell
        let currentCell = collectionView?.visibleCells.last as! ZJPictureBrowserCell
        
        currentCell.downloadAction()

    }
    
    //MARK:显示本VC
    public func show(){
    
        presentingVC?.present(self, animated: true, completion: nil)
    }
}
extension ZJPictureBrowser : UICollectionViewDelegate{
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
    
    }
    
    
    public func scrollViewWillBeginDragging(_ scrollView: UIScrollView){
    


        currentCell = collectionView?.visibleCells.last as? ZJPictureBrowserCell

     
        currentCell?.restore()
        
    }
    
    public func scrollViewDidEndDragging(_ scrollView: UIScrollView, willDecelerate decelerate: Bool){
       
    }
}

extension ZJPictureBrowser : UICollectionViewDataSource{
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
    
        return  pictureUrls.isEmpty ?  pictureNames.count : pictureUrls.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
  
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NSStringFromClass(ZJPictureBrowserCell.self), for: indexPath) as! ZJPictureBrowserCell
        
      
        cell.delegate = self
        
        cell.index = indexPath.row

        if pictureUrls.isEmpty {
            

            cell.imageStr = pictureNames[indexPath.row]
            
        }else{
        
            let pictureUrl = pictureUrls[indexPath.row]
            
            cell.imageUrlStr = pictureUrl
            
            
        }

        return cell
    }
    
    public func numberOfSections(in collectionView: UICollectionView) -> Int{
    
        return 1
    }
    
}

extension ZJPictureBrowser : ZJPictureBrowserCellDelegate{
    
    func singleTapAction(index:Int) {
        //self.dismiss(animated: true, completion: nil)
    }
    
}
